#include "stdafx.h"
#include "GateServer.h"

GateServer::GateServer(void)
{
	LOGGER_SET_FILE_PREFIX("GateServer");
	LOGGER_INFO("GateServer");
	m_pSocketServer = NULL;
}

GateServer::~GateServer(void)
{
}
void GateServer::OnExit()
{
}

void GateServer::OnTimer(UInt32 nTimerId)
{
	LOGGER_INFO("nTimerId:%d",nTimerId);
}
std::string GateServer::GetServerPath()
{
	std::string strPath;
	char path[280] = { 0 };
	Int32 len = 0;

#ifdef _WIN32
	len = ::GetModuleFileNameA(0, path, MAX_PATH);
#else
	FILE* stream = fopen("/proc/self/cmdline", "r");
	fgets(path, 280, stream);
	fclose(stream);
	len = strlen(path);

	std::cout << "server path:" << path << std::endl;
#endif

	if(len > 0)
	{
		while(len > 0 && path[len] != '\\')
			len--;

		if(path[len] == '\\')
			path[len + 1] = 0;

		strPath.assign(path, len + 1);
	}
	return strPath;
}


//void GateServer::SendDataToDataServer(const void* pData,size_t nDataLen)
//{
//	static ISocketClient* pSocketClient = singleton<ClientCallbackGame>::instance().getSocketClient();
//	void   *p = const_cast<void*>(pData);
//	pSocketClient->SendData((char*)p+sizeof(UInt32),nDataLen-sizeof(UInt32));
//}

Int32 GateServer::Run()
{
	std::string path = singleton<GateServer>::instance().GetServerPath();
	std::string filename = path + "GateServer.ini";

#ifndef _WIN32
	filename = "./GateServer.ini";
#endif

	if(singleton<Config>::instance().LoadFromFile(filename.c_str()) == false)
	{
		LOGGER_INFO("not found GateServer.ini!");
		return 0;
	}

	//-------------------------------------------------------------
	ISocketServer* pSocketServer = CreateSocketServer();
	if(pSocketServer == NULL)
	{
		LOGGER_INFO("pSocketServer == NULL");
		return 0;
	}

	singleton<ServerCallbackGate>::instance().SetSocketServer(pSocketServer);

	std::string strIp = singleton<Config>::instance().GetItem("gate-ip");
	std::string strPort = singleton<Config>::instance().GetItem("gate-port");
	LOGGER_INFO("gate server,ip:%s,port:%s",strIp.c_str(),strPort.c_str());

	pSocketServer->Run(strIp.c_str(),strPort.c_str(),&(singleton<ServerCallbackGate>::instance()));
	
	//-------------------------------------------------------------
	ISocketClient* pSocketClientGame = CreateSocketClient();
	if(pSocketClientGame == NULL)
	{
		LOGGER_INFO("pSocketClientData == NULL");
		return 0;
	}

	std::string strGameIp = singleton<Config>::instance().GetItem("game-ip");
	std::string strGamePort = singleton<Config>::instance().GetItem("game-port");
	LOGGER_INFO("data server,ip:%s,port:%s ",strGameIp.c_str(),strGamePort.c_str());

	singleton<ClientCallbackGame>::instance().setSocketClient(pSocketClientGame);
	while(!pSocketClientGame->Connect(strGameIp.c_str(),strGamePort.c_str(),&(singleton<ClientCallbackGame>::instance()),true))
	{
		LOGGER_INFO("connect game server failed! ip:%s,port:%s",strGameIp.c_str(),strGamePort.c_str());
		_Sleep(1000);
	}
	LOGGER_INFO("connect game server success! ip:%s,port:%s",strGameIp.c_str(),strGamePort.c_str());

	while(1)
	{
		singleton<TimerManager>::instance().Update();
		pSocketClientGame->Update();
		pSocketServer->Update();
		_Sleep(1);
	}
	singleton<GateServer>::instance().OnExit();
}

int main(int argc, char* argv[])
{
	LOGGER_SET_FILE_PREFIX("GateServer");
	singleton<GateServer>::instance().Run();
	return 0;
}
